package com.example.im.ui.adapter

import android.content.Context
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.im.R
import com.example.im.bean.ChatEntry
import com.example.im.bean.UserRoom
import com.example.im.common.Constants
import com.xlkj.ehobe.storage.ui.adapter.holder.RecyclerViewHolder
import kotlinx.android.synthetic.main.recycler_item_chat_user.view.*
import java.lang.StringBuilder
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.abs
import kotlin.math.min


class ChatUserAdapter2(
    private val mContext: Context,
    private val entrys: MutableList<ChatEntry>
) :
    RecyclerView.Adapter<RecyclerViewHolder>() {


    private var mOnClickItemListener: OnClickItemListener? = null

    fun setOnItemClickListener(mOnClickItemListener: OnClickItemListener) {
        this.mOnClickItemListener = mOnClickItemListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        return RecyclerViewHolder(
            LayoutInflater.from(mContext).inflate(R.layout.recycler_item_chat_user, parent, (false))
        )
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {

        val view: View = holder.itemView

        val entry = entrys[position]

        val userRoom = entry.userRoom
        val chatLast = entry.chatLast
        val chatRecord = entry.chatRecord

        //赋值用户名
        view.tv_name.text = userRoom?.nickname!!

        //赋值消息内容
        val body = StringBuilder()
        if (chatLast?.isAit!!)
            body.append("<font color=#ff0000>[有人@你]</font>")
        body.append(chatRecord?.body)
        view.tv_text.text = Html.fromHtml(body.toString())

        //赋值时间
        view.tv_time.text = SimpleDateFormat("MM-dd HH:mm", Locale.getDefault())
            .format(Date(chatRecord?.time!!))

        //赋值头像
        val icon = if (chatRecord.kind == Constants.CHAT_KIND_1) {
            R.drawable.ic_touxiang
        } else {
            R.drawable.ic_group_touxiang
        }
        view.img_user_heard.setImageResource(icon)

        //赋值未读消息
        val visibility = if (entry.unread > 0) {
            view.tv_count.text = if (entry.unread > 100)
                "···" else entry.unread.toString()
            View.VISIBLE
        } else {
            View.GONE
        }
        view.tv_count.visibility = visibility

        //添加条目监听
        view.setOnClickListener { v ->
            mOnClickItemListener!!.onClickItem(v, position)
        }
        view.tag = position
    }

    override fun getItemCount(): Int {
        return entrys.size
    }

    fun addDataItem(entry: ChatEntry) {
        if (entrys.none { it.chatLast?.id == entry.chatLast?.id }) {
            this.entrys.add(0, entry)
            this.notifyDataSetChanged()
        } else {
            for ((index, value) in this.entrys.withIndex()) if (value.chatLast?.id == entry.chatLast?.id) {
                this.entrys[index] = entry
                for (i in index downTo 1) {
                    Collections.swap(this.entrys, i, i - 1)
                }
                this.notifyItemRangeChanged(min(index, 0), abs(index - 0) + 1)
                return
            }
        }
    }

    fun upDataItem(entry: ChatEntry) {
        for ((index, value) in this.entrys.withIndex()) if (value.chatLast?.id == entry.chatLast?.id) {
            this.entrys[index] = entry
            this.notifyItemChanged(index)
            return
        }
    }

    fun removeDataItem(userRoom: UserRoom) {
        for ((index, value) in this.entrys.withIndex()) if (value.userRoom?.id == userRoom.id) {
            this.entrys.removeAt(index)
            this.notifyItemRangeChanged(index, itemCount + 1)
            return
        }
    }

    fun clearData() {
        this.entrys.clear()
        this.notifyDataSetChanged()
    }


    interface OnClickItemListener {
        fun onClickItem(view: View, position: Int)
    }
}
